import tools from '@/utils/tools'
import CRC from '@/utils/crc16'
import store from '@/store'
import { aesDecrypt } from '@/secret/secret'
import addressListDeal from './socketOnMessage/addressList'
import privateChat from './socketOnMessage/privateChat'
import groupChat from './socketOnMessage/groupChat'
import conference from './socketOnMessage/conference'
import other from './socketOnMessage/other'
import operateSuccess from './socketOnMessage/operateSuccess'
import operateFailed from './socketOnMessage/operateFailed'

const dataDealer = tools.dataDealer

export const websocketonmessage = async (e) => {
  const res = e.data
  if (res) {
    // 获取版本号
    let version = dataDealer.uint8Intercept(res, 0, 2)
    // 获取请求编号
    let reqId = dataDealer.uint8Intercept(res, 2, 4)
    // 获取消息类型
    let msgType = dataDealer.uint8Intercept(res, 6, 1)
    // 获取子类型1
    let subType1 = dataDealer.uint8Intercept(res, 7, 1)
    // 获取子类型2
    let subType2 = dataDealer.uint8Intercept(res, 8, 1)
    // 获取数据长度
    let bodyLength = dataDealer.uint8Intercept(res, 9, 4)
    // 获取body数据
    const body = dataDealer.uint8Intercept(res, 13, res.byteLength - 15)
    // 获取crc
    const crc = dataDealer.uint8Intercept(res, res.byteLength - 2, 2)
    // 获取前七项的合并uint8array
    const sevenBuffer = dataDealer.uint8Intercept(res, 0, res.byteLength - 2)
    let myCrc = CRC.ToCRC16(sevenBuffer)
    myCrc = dataDealer.numToUint8(myCrc, Uint16Array)
    // 判断myCRC与res中的crc是否相等
    if (JSON.stringify(myCrc) === JSON.stringify(crc)) {
      // 转换出version uint8array => str
      version = dataDealer.uint8ToStr(version)
      // 转换出请求编号 uint8array => uint32 => num
      reqId = dataDealer.uint8ToNum(reqId, 32)
      // 转换出消息类型 uint8array => num
      msgType = dataDealer.uint8ToNum(msgType, 8)
      // 转换出子类型1 uint8array => num
      subType1 = dataDealer.uint8ToNum(subType1, 8)
      // 转换出子类型2 uint8array => num
      subType2 = dataDealer.uint8ToNum(subType2, 8)
      // 转换出body数据长度
      bodyLength = dataDealer.uint8ToNum(bodyLength, 32)
      const type = msgType + '.' + subType1 + '.' + subType2
      const result = {
        version,
        reqId,
        msgType,
        subType1,
        subType2,
        bodyLength,
        body: body.length && store.state.common.DHSecretKey ? aesDecrypt(body, store.state.common.DHSecretKey) : body,
        crc
      }
      store.commit('socket/set_reqList_removeKeyValue', reqId)
      if (type !== '5.1.3') {
        // 心跳
        console.log(result)
      }
      switch (msgType) {
        case 1: {
          addressListDeal(type, result)
          break
        }
        case 2: {
          privateChat(type, result)
          break
        }
        case 3: {
          groupChat(type, result)
          break
        }
        case 4: {
          conference(type, result)
          break
        }
        case 5: {
          other(type, result)
          break
        }
        case 255: {
          switch (subType1) {
            case 1: {
              operateSuccess(result)
              break
            }
            case 0: {
              operateFailed(type, result)
              break
            }
          }
          break
        }
        default: {
          break
        }
      }
    }
  }
}
